<?php

namespace App\Http\Controllers\Managea;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\ProductRepository;
use App\Repositories\CategoryRepository;
use App\Traits\BaseFun;

class PackageController extends Controller
{
    public function __construct(
        ProductRepository $ProductRepository,
        CategoryRepository $CategoryRepository
    ) {
        $this->ProductRepository = $ProductRepository;
        $this->CategoryRepository = $CategoryRepository;
    }




    public function Packagelist(Request $request)
    {
        $where = ['tc'=> 1];
        if ($request->search) {
            $where['search'] = $request->search;
        }
        $res = $this->ProductRepository->pageList($where);
        if ($res) {
            $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        } else {
            $ArrDate = ['state' => 2,  'data' => $res, 'msg' => '获取失败!'];
        }
        return BaseFun::jsonReturn($ArrDate);
    }

    # 添加展示
    public function PackageAddList(Request $request)
    {
        $id = $request->id;
        $pro = $this->ProductRepository->list(['category' => $id]);
        $category = $this->CategoryRepository->list();

        $data = [
            'product' => $pro,
            'category' => $category,
        ];
        if ($pro) {
            $ArrDate = ['state' => 0, 'data' => $data, 'token' => $request->input('token')];
        } else {
            $ArrDate = ['state' => 2,  'data' => $data, 'msg' => '创建失败!'];
        }

        return BaseFun::jsonReturn($ArrDate);
    }

    # 添加方法
    public function PackageAdd(Request $request)
    {
        if($request->proid) {
            $proid = explode(',', $request->proid);
            $product = $this->ProductRepository->whereInlist(['id' => 'id', 'arr'=>$proid]);
            foreach ($product as $key => $value) {
                $pro_name[] = $value['name'];
            }
            $name = implode('+', $pro_name);
            $request->offsetSet('name', $name);
        }

        $add = $this->ProductRepository->create($request->all());
        if ($add) {
            $ArrDate = ['state' => 0, 'data' => $add, 'token' => $request->input('token')];
        } else {
            $ArrDate = ['state' => 2,  'data' => $add, 'msg' => '创建失败!'];
        }
        return BaseFun::jsonReturn($ArrDate);
    }



    # 修改展示
    public function PackageInfo(Request $request, $id)
    {
        $res = $this->ProductRepository->info(['id'=>$id]);
        $proId_Arr = explode(',', $res['proid']);

        $res['pro'] = $this->ProductRepository->whereInlist(['id' => 'id', 'arr'=>$proId_Arr]);

        if ($res) {
            $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        } else {
            $ArrDate = ['state' => 2,  'data' => $res, 'msg' => '获取失败!'];
        }
        return BaseFun::jsonReturn($ArrDate);
    }

    # 修改提交
    public function PackageEdit(Request $request)
    {   
        $Proinfo = $this->ProductRepository->info(['id'=>$request->id]);
        
        if($request->proid  &&  $request->proid != $Proinfo['proid']) {
            $proid = explode(',', $request->proid);

            $product = $this->ProductRepository->whereInlist(['id' => 'id', 'arr'=>$proid]);
            foreach ($product as $key => $value) {
                $pro_name[] = $value['name'];
            }
            $name = implode('+', $pro_name);

            $request->offsetSet('name', $name);
        }

        $up = $this->ProductRepository->update($request->all());
        if ($up) {
            $ArrDate = ['state' => 0, 'data' => $up, 'token' => $request->input('token')];
        } else {
            $ArrDate = ['state' => 2,  'data' => $up, 'msg' => '修改失败!'];
        }

        return BaseFun::jsonReturn($ArrDate);
    }
}
